<?php

namespace App\Admin\Controllers;

use App\Models\ArticleCat;
use App\Models\ArticleCatAdmin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;

class ArticleCatController extends BaseController
{
    private $states = [
        'on' => ['value' => 1, 'text' => '有效', 'color' => 'success'],
        'off' => ['value' => 0, 'text' => '无效', 'color' => 'danger'],
    ];


    public function __construct()
    {
    }

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('文章类别')
            ->description(' ')
            ->body($this->tree());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('类别详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改')
            ->description(' ')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        //一级分类
        $parentCat = $this->articleCatDao->pArticleCat();

        $grid = new Grid(new ArticleCat);
        $grid->filter(function($filter) use($parentCat) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/2, function ($filter){
                $filter->where(function ($query) {
                    $this->input = trim($this->input);
                    $query->where('id','=',"{$this->input}");
                }, 'ID');

                $filter->where(function ($query) {
                    $this->input = trim($this->input);
                    $query->where('name', 'like', "%{$this->input}%");
                }, '名称');
            });

            $filter->column(1/2, function ($filter) use($parentCat) {
                $filter->equal('parent_id','所属父类')->select($parentCat);
            });
        });

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id','ID')->sortable();
        $grid->column('name','栏目名称');
        $grid->column('parent_id','所属父类')->using($parentCat);
        $grid->column('rank','排序');
        $grid->column('desc','分类描述');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ArticleCat::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ArticleCat());
        $form->select('parent_id', '上级')->options(ArticleCatAdmin::selectOptions());
        $form->text('name','分类名称')->rules('required');
        $form->number('order','排序');
        $form->text('desc','分类描述')->default('')->required();
        //保存前回调
        $form->saving(function (Form $form) {
//            $form->model()->keywords = "";
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
    protected function tree()
    {
        return ArticleCatAdmin::tree(function (Tree $tree) {

            $tree->branch(function ($branch) {

                $src = $branch['icon'];

                $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";

                return "{$branch['id']}  - {$branch['name']} {$logo}";

            });
        });
    }
}
